/**
 * Created by wenguangjia on 2017/4/20.
 */
(function () {
    'use strict';

    angular
        .module('logcenterApp')
        .controller('deployLogController', deployLogController)
    deployLogController.$inject = ['$scope','searchService'];
    function deployLogController($scope,searchService) {
        // var array=new Array();//数据源
        // var lastDeploy=null;
        // var isLoadMore=true;//如果为false则不自动加载

        // $scope.param={name:null,userid:null,type:null,"@timestamp":null,endTime:new Date(),timestamp:null,deploytype:"H"};
        $scope.param={name:null,userid:null,type:null,"@timestamp":null,endTime:getToday11Clock(),timestamp:null,partition:null,area:null};
        $scope.isSuccess="all";
        var queryAction="";
        $scope.query=function(){
            // $scope.param.page=0;
            // lastDeploy=null;
            // array.length=0;
            queryAction="1";
            $scope.param["@timestamp"]=$scope.param.timestamp;
            // if($scope.param.name==""){
            //     $scope.param.name=null
            // }
            // if($scope.param.userid==""){
            //     $scope.param.userid=null
            // }
            $scope.deployGridOptions.dataSource.read();
        };


        /**
         * grid
         * @type {*[]}
         */
        var columns = [
            {field: "file_name",title:"文件名",width:"84px",hidden:true},
            {field: "name",title:"程序名",width:"90px"},
            {field: "deploytype", title: "类型",width:"80px"},
            {field: "userid", title: "用户名",width:"84px"},
            {field: "timestamp", title: "时间",width: "164px"},
            // {field: "timestamp", title: "时间",format:"{0: yyyy-MM-dd HH:mm:ss}",width: "164px"},
            {
                field: "progress",
                title: "进度",
                width:"300px",
                template:function (dataItem) {
                    var progressStr=dataItem.parent().progress;
                    var array=progressStr.split('_');
                    $scope.uploadActive= (progressStr.indexOf("UPLOAD")!=-1);
                    $scope.compileActive=(progressStr.indexOf("COMPILE")!=-1);
                    $scope.rebootActive=(progressStr.indexOf("REBOOT")!=-1);
                    $scope.successActive=(progressStr.indexOf("SUCCESS")!=-1);
                    var html=
                        '<ul class="progressbar msform">' +
                            '<li ng-class="uploadActive?active:not-active">UPLOAD</li>' +
                            '<li ng-class="compileActive?active:not-active">COMPILE</li> ' +
                            '<li ng-class="rebootActive?active:not-active">REBOOT</li>' +
                            '<li ng-class="successActive?active:not-active">SUCCESS</li>' +
                        '</ul>';

                    //var result='';
                    var result='<ul class="progressbar msform">';
                    angular.forEach(array,function (item, i) {
                        result+='<li class="active">'+kendo.htmlEncode(item)+'</li>' ;
                        /*if(item.toUpperCase()=="SUCCESS"){
                            result+='<span>'+kendo.htmlEncode(item)+'&nbsp;</span>';
                        }else {
                            result +='<span>'+kendo.htmlEncode(item)+'&nbsp;</span><i class="fa fa-long-arrow-right" aria-hidden="true"></i>';
                        }*/
                    });
                    if(array.length==1){
                        result+=
                            '<li>COMPILE</li>' +
                            '<li>REBOOT</li>' +
                            '<li>SUCCESS</li>';
                    }
                    if(array.length==2){
                        result+=
                            '<li>REBOOT</li>' +
                            '<li>SUCCESS</li>';
                    }
                    if(array.length==3){
                        result+=
                            '<li>SUCCESS</li>';
                    }
                    result+='</ul>';
                    /*if(progressStr.indexOf("_SUCCESS")==-1){
                        result+='<span>FAILURE&nbsp;</span>';

                        result='<strong style="color: red;">'+result+'</strong>'
                    }else {
                        result='<strong style="color: green;">'+result+'</strong>'
                    }*/

                    return result;
                }
            }
        ];
        $scope.deployGridOptions={
            selectable: "row",
            scrollable: true,
            sortable: true,
            resizable: true,
            columns: columns,
            columnMenu:true,
            pageable: {
                refresh: true,
                pageSizes: true,
                buttonCount: 5
            }
        };
        // $scope.deployModel = kendo.data.Model.define({
        //     fields: {
        //         timestamp:{type: 'date'}
        //     }
        // });
        $scope.deployGridOptions.dataSource = new kendo.data.DataSource({
            autoBind: false,
            transport: {
                read: function (e) {



                },
                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return {models: kendo.stringify(options.models)};
                    }
                }
            },
            requestStart: function (e) {
                if (queryAction=="") {
                    e.preventDefault();
                }
            },
            // schema: {
            //     model: $scope.deployModel
            // },
            batch: true,
            pageSize: 20
        });

        function getDate(str){
            if(str==null||str==""){
                return "";
            }
            var oDate = new Date(str),
                oYear = oDate.getFullYear(),
                oMonth = oDate.getMonth()+1,
                oDay = oDate.getDate(),
                oHour = oDate.getHours(),
                oMin = oDate.getMinutes(),
                oSen = oDate.getSeconds(),
                oMill=oDate.getMilliseconds();
               var oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen)+"."+getzf2(oMill);//最后拼接时间
            return oTime;
        };
        //补0操作
        function getzf(num){
                if (parseInt(num) < 10) {
                    num = '0' + num;
                }
            return num;
        }
        function getzf2(num){
            if (parseInt(num) < 10) {
                num = '00' + num;
            }else if(parseInt(num) < 100){
                num = '0' + num;
            }
            return num;
        }
       /* //文档高度
        function getDocumentTop() {
            var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
            if (document.body) {
                bodyScrollTop = document.body.scrollTop;
            }
            if (document.documentElement) {
                documentScrollTop = document.documentElement.scrollTop;
            }
            scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
            return scrollTop;
        }
        //可视窗口高度
        function getWindowHeight() {
            var windowHeight = 0;
            if (document.compatMode == "CSS1Compat") {
                windowHeight = document.documentElement.clientHeight;
            } else {
                windowHeight = document.body.clientHeight;
            }
            return windowHeight;
        }
        //滚动条滚动高度
        function getScrollHeight() {
            var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
            if (document.body) {
                bodyScrollHeight = document.body.scrollHeight;
            }
            if (document.documentElement) {
                documentScrollHeight = document.documentElement.scrollHeight;
            }
            scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
            return scrollHeight;
        }
        window.onscroll = function () {
            //监听事件内容
            if((getDocumentTop() + getWindowHeight() == getScrollHeight())&&isLoadMore){
                //当滚动条到底时,这里是触发内容
                //异步请求数据,局部刷新dom
                //requestData.from+=sourceCount;
                $scope.param.page++;
                $scope.deployGridOptions.dataSource.read();
            }
        }*/
        /**
         * 分区配置
         * @type {string}
         */
        $scope.partition = '全部';
        var partitionSrc="";
        searchService.getPartition({}).then(
            function(resp){
                partitionSrc=resp.data;
                $scope.partitionSource=toTree(resp.data);
            },function(err){}
        );
        $scope.selectShow=function(item){
            if(item==0){
                $scope.param.area=null;
                $scope.param.partition=null;
                $scope.partition="全部";
            }
            angular.forEach(partitionSrc,function(k){
                if(item.ename==k.ename){
                    $scope.param.area=k.code;
                    $scope.param.partition=k.ename;
                    $scope.partition=item.fname+"-"+item.cname;
                }
            });
            $scope.win.close();
        };
    }


})();

